-- Platform
-- Created by chengb Jul/3/2015
-- 平台相关

local targetPlatform = cc.Application:getInstance():getTargetPlatform();

-- 是否是安卓平台
function isAndroidPlatform()
    return cc.PLATFORM_OS_ANDROID == targetPlatform;
end

-- 是否是windows平台
function isWindowsPlatform()
    return cc.PLATFORM_OS_WINDOWS == targetPlatform;
end

-- 是否是iphone平台
function isIphonePlatform()
    return cc.PLATFORM_OS_IPHONE == targetPlatform;
end

-- 是否是ipad平台
function isIpadPlatform()
    return cc.PLATFORM_OS_IPAD == targetPlatform;
end

-- 是否IOS平台
function isIosPlatform()
    return isIphonePlatform() or isIpadPlatform();
end

-- 是否chromebook
function isChromeBook()
    if isAndroidPlatform() then
        return callJavaFunc("isChromeBook", {}, "()Z");
    else
        return false;
    end
end

-- 获取平台名称
function getPlatformName()
    if isAndroidPlatform() then
        return "android";
    elseif isIosPlatform() then
        return "ios";
    else
        return "other";
    end
end

-- 获取平台名称
function getPlatformName2()
    if isAndroidPlatform() then
        return "android";
    elseif isIphonePlatform() then
        return "iphone";
    elseif isIpadPlatform() then
        return "ipad";
    elseif isWindowsPlatform() then
        return "windows";
    else
        return "other";
    end
end

-- 退出應用
function exitApp()
    cc.Director:getInstance():endToLua();
    if isIphonePlatform() then
        os.exit(0);
    end
end

---------------------
-- 打开一个超链接
-- @param url 超链接地址
function openUrl(url)
    cclog("openUrl: %s", url);
    cc.Application:getInstance():openURL(url);
end

------------------------
-- 退出系统
-- @param
function exitSystem()
    print("exit");
    if isAndroidPlatform() then
        -- 需要设置推送
        setLocalPush();
        -- 调用java的退出
        callJavaFunc("exitSystem", {}, "()V");
    else
        exitApp();
    end
end

-- 退出系统
function simpleExitSystem()
    print("simpleExitSystem");
    if isAndroidPlatform() then
        callJavaFunc("simpleExitSystem", {}, "()V");
    end
end

-- 重启应用
function restartApp()
    print("restartApp");
    if isAndroidPlatform() then
        callJavaFunc("restartApp", {}, "()V");
    else
        -- TODO 其他平台直接关闭
        exitApp();
    end
end

-- 获取应用id
function getBundleId()
    if isAndroidPlatform() then
        return callJavaFunc("getBundleId", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        return callOCFunc("getBundleId");
    else
        return "";
    end
end

-- 调用本地确认框
function nativeConfirm(title, msg, okText, cancelText, okFunc, cancelFunc)
    if isAndroidPlatform() then
        callJavaFunc("nativeConfirm",
            {title, msg, okText, cancelText, okFunc, cancelFunc},
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)V");
    elseif isIosPlatform() then
        callOCFunc("nativeConfirm",
            {title=title, msg=msg, okText=okText, cancelText=cancelText,
            okFunc=okFunc, cancelFunc=cancelFunc});
    else
        print("尚未实现nativeConfirm功能");
        -- 暂时默认直接走确认流程
        okFunc();
    end
end

-- 调用本地提示框
function nativeAlert(title, msg, okText, okFunc)
    if isAndroidPlatform() then
        callJavaFunc("nativeAlert",
            {title, msg, okText, okFunc}, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
    elseif isIosPlatform() then
        callOCFunc("nativeAlert",
            {title=title, msg=msg, okText=okText, okFunc=okFunc});
    else
        print("尚未实现nativeAlert功能");
        okFunc();
    end
end

-- 获取设备唯一码
function getDeviceId()
    if isAndroidPlatform() then
        -- 安卓下设备唯一码的生成规则为：mac + imei
        local mac  = NetworkStatus.getMacAddress();
        local imei = getIMEI();
        return mac .. imei;
    elseif isIosPlatform() then
        -- ios下的设备唯一码
        return callOCFuncWithClass("QCToolkit", "keychainUUID");
    end

    return "";
end

-- 获取IMEI
function getIMEI()
    if isAndroidPlatform() then
        return callJavaFunc("getIMEI", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        return callOCFuncWithClass("QCToolkit", "idfa");
    end

    return "";
end

-- 获取系统版本号
function getOSVersion()
    if isAndroidPlatform() then
        return callJavaFunc("getOSVersion", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        return callOCFuncWithClass("QCToolkit", "systemVersion");
    end

    return "";
end

-- 获取手机型号
function getMobileModel()
   if isAndroidPlatform() then
        return callJavaFunc("getMobileModel", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        return callOCFuncWithClass("QCToolkit", "machineName");
    end

    return "";
end

-- 获取应用程序版本
function getBundleVersion()
    local ret = "";
    if isAndroidPlatform() then
        return callJavaFunc("getBundleVersion", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        ret = callOCFuncWithClass("QCToolkit", "bundleVersion");
    end

    return ret;
end


